package zzs.generalframework.console.core.parameter.commonparameter.operatingpost.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.parameter.commonparameter.operatingpost.OperatingPostBO;
import zzs.generalframework.common.core.parameter.commonparameter.operatingpost.OperatingPostInfo;
import zzs.generalframework.common.core.parameter.commonparameter.operatingpost.OperatingPostPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.parameter.commonparameter.operatingpost.dao.IOperatingPostDao;
import zzs.generalframework.console.core.parameter.commonparameter.operatingpost.service.IOperatingPostService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.operatingpost.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.parameter.commonparameter.operatingpost.service.impl.operatingPostServiceImpl")
public class OperatingPostServiceImpl implements IOperatingPostService
{

	private Logger logger = LoggerFactory.getLogger(OperatingPostServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IOperatingPostDao operatingPostDao;

	@Override
	public ConsoleReturnBO saveOperatingPost(OperatingPostBO operatingPostBO)
	{
		logger.debug("====保存工作岗位信息开始====");
		logger.debug("====保存工作岗位信息.入参:OperatingPostBO=[{}]====", operatingPostBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		OperatingPostPO operatingPostPO = new OperatingPostPO();
		copyOperatingPostBO2OperatingPostPO(operatingPostBO, operatingPostPO);
		Boolean insertResult = operatingPostDao.insert(operatingPostPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存工作岗位信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存工作岗位信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveOperatingPost(List<OperatingPostBO> operatingPostBOList) throws ConsoleException
	{
		logger.info("====保存工作岗位信息列表开始====");
		logger.debug("====保存工作岗位信息列表.入参:OperatingPostBOList=[{}]====", operatingPostBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (OperatingPostBO itemBO : operatingPostBOList)
		{
			ConsoleReturnBO itemResult = saveOperatingPost(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存工作岗位信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存工作岗位信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getOperatingPost(OperatingPostInfo operatingPostInfo)
	{
		logger.info("====获取工作岗位信息开始====");
		logger.debug("====获取工作岗位信息.入参:OperatingPostInfo=[{}]====", operatingPostInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<OperatingPostPO> operatingPostPOList = operatingPostDao.select(operatingPostInfo);
		if (operatingPostPOList == null || operatingPostPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (operatingPostPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			OperatingPostBO operatingPostBO = new OperatingPostBO();
			copyOperatingPostPO2OperatingPostBO(operatingPostPOList.get(0), operatingPostBO);
			returnConsoleReturnBO.setReturnObj(operatingPostBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			OperatingPostBO operatingPostBO = new OperatingPostBO();
			copyOperatingPostPO2OperatingPostBO(operatingPostPOList.get(0), operatingPostBO);
			returnConsoleReturnBO.setReturnObj(operatingPostBO);
		}
		logger.debug("====获取工作岗位信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取工作岗位信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listOperatingPost(OperatingPostInfo operatingPostInfo)
	{
		logger.info("====获取工作岗位信息列表开始====");
		logger.debug("====获取工作岗位信息列表.入参:OperatingPostInfo=[{}]====", operatingPostInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<OperatingPostPO> operatingPostPOList = operatingPostDao.select(operatingPostInfo);
		if (operatingPostPOList == null || operatingPostPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<OperatingPostBO> resultList = new ArrayList<OperatingPostBO>();
			for (OperatingPostPO itemPO : operatingPostPOList)
			{
				OperatingPostBO itemBO = new OperatingPostBO();
				copyOperatingPostPO2OperatingPostBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取工作岗位信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取工作岗位信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateOperatingPost(OperatingPostBO operatingPostBO)
	{
		logger.info("====更新工作岗位信息开始====");
		logger.debug("====更新工作岗位信息.入参:OperatingPostBO=[{}]====", operatingPostBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		OperatingPostPO operatingPostPO = new OperatingPostPO();
		copyOperatingPostBO2OperatingPostPO(operatingPostBO, operatingPostPO);
		Boolean updateResult = operatingPostDao.update(operatingPostPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新工作岗位信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新工作岗位信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateOperatingPost(List<OperatingPostBO> operatingPostBOList) throws ConsoleException
	{
		logger.info("====更新工作岗位信息列表开始====");
		logger.debug("====更新工作岗位信息列表.入参:List<OperatingPostBO>=[{}]====", operatingPostBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (OperatingPostBO itemBO : operatingPostBOList)
		{
			ConsoleReturnBO itemResult = updateOperatingPost(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新工作岗位信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新工作岗位信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeOperatingPost(OperatingPostBO operatingPostBO)
	{
		logger.info("====删除工作岗位信息开始====");
		logger.debug("====删除工作岗位信息.入参:OperatingPostBO＝[{}]======", operatingPostBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		OperatingPostPO operatingPostPO = new OperatingPostPO();
		copyOperatingPostBO2OperatingPostPO(operatingPostBO, operatingPostPO);
		Boolean deleteResult = operatingPostDao.delete(operatingPostPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除工作岗位信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除工作岗位信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeOperatingPost(List<OperatingPostBO> operatingPostBOList) throws ConsoleException
	{
		logger.info("====删除工作岗位信息列表开始====");
		logger.debug("====删除工作岗位信息列表.入参:List<OperatingPostBO>＝[{}]======", operatingPostBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (OperatingPostBO itemBO : operatingPostBOList)
		{
			ConsoleReturnBO itemResult = removeOperatingPost(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除工作岗位信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除工作岗位信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countOperatingPost(OperatingPostBO operatingPostBO)
	{
		logger.info("====统计工作岗位信息开始====");
		logger.debug("====统计工作岗位信息。入参：OperatingPostBO＝[{}]======", operatingPostBO);
		Long returnLong = new Long(0);
		OperatingPostPO operatingPostPO = new OperatingPostPO();
		copyOperatingPostBO2OperatingPostPO(operatingPostBO, operatingPostPO);
		returnLong = operatingPostDao.count(operatingPostPO);
		logger.debug("====统计工作岗位信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计工作岗位信息结束====");
		return returnLong;
	}

	private void copyOperatingPostBO2OperatingPostPO(OperatingPostBO operatingPostBO, OperatingPostPO operatingPostPO)
	{
		beanUtils.copyBeanNotNull(operatingPostBO, operatingPostPO);
		if (operatingPostBO.getSysID() != null)
		{
			operatingPostPO.setSeqID(operatingPostBO.getSysID());
		}
	}

	private void copyOperatingPostPO2OperatingPostBO(OperatingPostPO operatingPostPO, OperatingPostBO operatingPostBO)
	{
		beanUtils.copyBeanNotNull(operatingPostPO, operatingPostBO);
		if (operatingPostPO.getSeqID() != null)
		{
			operatingPostBO.setSysID(operatingPostPO.getSeqID());
		}
	}

}
